package com.herenit.order_model.utils;

import com.herenit.order_model.exception.MyException;
import com.herenit.order_model.model.Message;
import com.herenit.order_model.model.ResponseVO;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import java.io.UnsupportedEncodingException;
import java.util.Map;

public class ResponseVOUtils {

    private static ResponseVO response = new ResponseVO();

    public static ResponseVO noPermission(){
        response.setResult("没有权限");
        response.setMessage("失败");
        response.setCode("1");
        return response;
    }

    public static ResponseVO checkSendMessage(String message) {
        response.setMessage("发送：" + message);
        response.setCode("200");
        response.setResult("发送成功");
        return response;
    }

    public static ResponseVO checkConsumeMessage(Message message) throws UnsupportedEncodingException {
        response.setMessage(message.getMessage());
        response.setCode(message.getCode());
        if (message.getResult() != null) {
            if (message.getResult() instanceof ConsumerRecords) {
                if (!((ConsumerRecords) message.getResult()).isEmpty()){
                    ConsumerRecords<String, Object> records = (ConsumerRecords<String, Object>) message.getResult();
                    response.setResult(records.iterator().next().value());
                }else {
                    response.setResult(null);
                }
            } else {
                Map map = (Map) message.getResult();
                map.remove("recordId");
                response.setResult(map);
            }
        } else {
            response.setResult(null);
        }
        return response;
    }

    public static ResponseVO querySuccess(Object object){
        response.setCode("200");
        response.setMessage("查询所有打回记录：查询成功");
        response.setResult(object);
        return response;
    }

    public static ResponseVO handlerException(MyException ex){
        response.setCode(ex.getCode());
        response.setMessage(ex.getMessage());
        response.setResult(null);
        return response;
    }
}
